package types

import (
	"math"

	"github.com/golang/protobuf/proto"
	"hundsun.com/hsl/hschain/common/crypto"
	"hundsun.com/hsl/hschain/common/util/pb"
	pbledger "hundsun.com/hsl/hschain/protos/ledger"
)

// EstimateMaxBlockHeaderSizeBytes 估算区块头最大大小，算出的值向上取整直到是1024倍数
func EstimateMaxBlockHeaderSizeBytes() int {
	kp, err := crypto.KeyPairGen()
	if err != nil {
		panic(err)
	}
	genesisBlk := pb.GetGenesisBlock()
	blkHeader := &pbledger.BlockHeader{
		ParentHash:            genesisBlk.Hash(),
		Number:                math.MaxUint64,
		Difficulty:            math.MaxUint64,
		BlockTime:             math.MaxInt64,
		CheckBlock:            math.MaxUint64,
		StateHashOfCheckBlock: genesisBlk.Hash(),
		BlockSequence:         math.MaxUint64,
	}
	genesisBlk.Sign(kp)
	blkHeaderBytes, _ := proto.Marshal(blkHeader)
	return ((len(blkHeaderBytes) + 1023) / 1024) * 1024
}
